<!--
@author  tanyuze
@desc    完成条件
@date    2022/11/24
-->
<template>
  <div class="join-condition">
    <el-collapse-transition>
      <section v-show="searchShow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.paramName"
          placeholder="输入名称搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <!--          <span class="search-more" @click="searchShow = !searchShow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>-->
        </section>
      </section>
    </el-collapse-transition>
    <el-collapse-transition>
      <section v-show="!searchShow" class="main-search separate">
        <section class="search-inner">
          <el-row>
            <el-col :span="12">
              <section class="search-inner-item">
                <div class="item-label">新闻标题</div>
                <el-input
                  v-model.trim="search.title"
                  style="width: 90%;"
                  class="filter-item"
                  placeholder="请输入新闻标题搜索"
                />
              </section>
            </el-col>
          </el-row>
        </section>
        <section class="search-btn">
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchShow = !searchShow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </section>
      </section>
    </el-collapse-transition>

    <div class="page-btn">
      <div class="page-title">
        <span>关联条件</span>
      </div>
      <div class="page-btns">
        <el-button style="margin-left: 10px;" @click="handleCreated">新建 </el-button>
        <el-button style="margin-left: 10px;" @click="handleBack">返回 </el-button>
      </div>
    </div>

    <div class="page-main-list">
      <v-table
        :list="list"
        tag="honorConection"
        :source-arr="sourceArr"
        :selection="true"
        :list-loading="listLoading"
        :filter-column="filterColumn"
        @handleSelectionChange="handleSelectionChange"
      >
        <el-table-column fixed="right" label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </v-table>
    </div>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getPage" />

    <!--关联条件-->
    <el-dialog
      v-el-drag-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      title="关联条件"
      center
      width="800px"
      append-to-body
      custom-class="condition"
      @close="closeDialog"
    >
      <el-form
        ref="userInfo"
        v-loading="submitloading"
        :model="honorInfo"
        :rules="rules"
        class="esccrow-form"
        label-position="right"
        label-width="100px"
      >
        <el-form-item v-if="fixed" label="选择数据">
          <div class="select-data" @click="getData">选择数据</div>
          <div>
            <el-tag v-for="tag in honorInfo.dataIds" :key="tag.dataId" closable @close="handleDelTag(tag)">
              {{ tag.dataName }}
            </el-tag>
          </div>
        </el-form-item>
        <el-form-item v-if="!fixed && showConditions" label="完成条件" prop="finishConditions">
          <el-radio-group v-model="honorInfo.finishConditions">
            <el-radio
              v-for="v in honorType"
              :key="v.value"
              :label="v.value"
            >{{ v.name }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="showFinish" label="完成值" prop="finishValue">
          <el-input
            v-model.trim="honorInfo.finishValue"
            placeholder="请输入完成值"
          >
            <template slot="append">{{ unit }}</template>
          </el-input>
        </el-form-item>
        <el-form-item label="关联荣誉" prop="honorName">
          <el-input v-model="honorInfo.honorName" :disabled="true" placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="getHonor">选择荣誉</el-button>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="default" style="padding: 10px 30px" @click="dialogVisible =false">取 消</el-button>
        <el-button type="primary" style="padding: 10px 30px" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <!--选择荣誉-->
    <el-dialog :close-on-click-modal="false" custom-class="live-dialog" :visible.sync="chooseHonor" title="选择荣誉" width="1000px" center>
      <choose-honor
        ref="chooseHonor"
        @select="select"
      />
      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="default" style="padding: 10px 30px" @click="chooseHonor =false">取 消</el-button>
        <el-button type="primary" style="padding: 10px 30px" @click="handleSubmit">确 定</el-button>-->
      </div>
    </el-dialog>

    <!--    选择资源-->
    <el-dialog
      v-if="chooseResources"
      :close-on-click-modal="false"
      :visible.sync="chooseResources"
      title="选择数据"
      width="1200px"
      center
    >
      <choose-resource
        ref="chooseResources"
        :key="chooseResources === true"
        :active-tab="seriesType"
        :selection="true"
        @selectMany="selectMany"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="default" style="padding: 10px 30px" @click="handleClose()">取 消</el-button>
        <el-button type="primary" style="padding: 10px 30px" @click="handleSelect">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { RelevanceInsert, RelevancePage, RelevanceDel } from '../api'
import elDragDialog from '@/directive/el-drag-dialog'
import vTable from '@/components/Table'
import chooseHonor from '../components/choosehonor'
import chooseResource from '../components/chooseResource'

export default {
  name: 'JoinCondition',
  components: { Pagination, vTable, chooseHonor, chooseResource },
  directives: { elDragDialog },
  data() {
    return {
      /* search*/
      searchShow: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        paramName: ''
      },
      search: {
        title: ''
      },

      /* table*/
      tag: '',
      list: [{}],
      listLoading: false,
      sourceArr: ['honorImgUrl'],

      /* user-defined*/
      chooseResources: false,
      activeTab: '',
      view: '/API-DOCUMENT/document/file/download?id=',
      ids: '',
      activityId: '',
      multipleSelection: [],
      dict: {
        DKZYLX: [],
        WCTJ: []
      },
      dialogVisible: false,
      submitloading: false,
      honorInfo: {
        finishConditions: '',
        finishValue: '',
        honorId: '',
        honorName: '',
        dataIds: []
      },
      seriesId: '',
      rules: {},
      chooseHonor: false,
      honorType: [
        {
          name: '累计',
          value: 0
        },
        {
          name: '连续',
          value: 1
        }
        /* {
          name: '指定',
          value: 2
        }*/
      ],
      unit: '分钟', // 单位
      showConditions: true, // 是否展示条件
      showFinish: true, // 受否展示完成值
      status: '',
      seriesType: '',
      fixed: false
    }
  },
  computed: {
    /* tag() {
      return this.tagList[this.activeTab]
    }*/
  },
  watch: {
    /* activeTab() {
      this.myTime = new Date().getTime()
    }*/
  },
  created() {
    if (!this.$route.query.i) {
      this.$message({
        showClose: true,
        message: '参数异常！',
        type: 'warning',
        onClose: () => {
          this.handleBack()
        }
      })
    }
    this.seriesId = this.$route.query.i
    this.seriesType = this.$route.query.seriesType
    // this.init()
    this.getPage()
  },
  methods: {
    init() {
      // 指定  次数
      if (this.seriesType === 'RYTJ_ZDCG') {
        this.fixed = true
        this.unit = '次数'
      }
      if (this.seriesType === 'RYTJ_ZDDTHDPM') {
        this.fixed = true
        this.unit = '名及以上'
      }
      // 指定  完成值 1
      if (this.seriesType === 'RYTJ_ZDPX' ||
          this.seriesType === 'RYTJ_ZDZB' ||
          this.seriesType === 'RYTJ_ZDWJ' ||
          this.seriesType === 'RYTJ_ZDTP') {
        this.fixed = true
        this.showFinish = false
        this.honorInfo.finishValue = 1
      }
      // 完成条件累计  完成值1
      if (this.seriesType === 'RYTJ_SMRZ' ||
          this.seriesType === 'RYTJ_SCBY' ||
          this.seriesType === 'RYTJ_SCYG' ||
          this.seriesType === 'RYTJ_SCERHS' ||
          this.seriesType === 'RYTJ_SCSRHS'
      ) {
        this.honorInfo.finishConditions = 0
        this.honorInfo.finishValue = 1
        this.showConditions = false
        this.showFinish = false
      }
      // 完成条件累计  单位数量
      if (this.seriesType === 'RYTJ_XZSL' ||
          this.seriesType === 'RYTJ_CHSL'
      ) {
        this.honorInfo.finishConditions = 0
        this.unit = '数量'
        this.showConditions = false
      }
      // 完成条件 累计  单位名次
      if (this.seriesType === 'RYTJ_JFZBMC'
      ) {
        this.honorInfo.finishConditions = 0
        this.unit = '名次'
        this.showConditions = false
      }

      // 累计  次数
      if (this.seriesType === 'RYTJ_ZBCS' ||
          this.seriesType === 'RYTJ_DTHD' ||
          this.seriesType === 'RYTJ_FXCS' ||
          this.seriesType === 'RYTJ_ERDZCS' ||
          this.seriesType === 'RYTJ_SRDZCS'
      ) {
        this.honorInfo.finishConditions = 0
        this.unit = '次数'
        this.showConditions = false
      }

      // 天数
      if (this.seriesType === 'RYTJ_QDCS' ||
          this.seriesType === 'RYTJ_YDSJ' ||
          this.seriesType === 'RYTJ_DTTS' ||
          this.seriesType === 'RYTJ_MRDT'
      ) {
        this.unit = '天数'
      }
    },

    filterColumn(scope, item) {
      if (item === 'finishConditions') {
        return scope.row[item] === 0 ? '累计' : scope.row[item] === 1 ? '连续' : '指定'
      }
      return scope.row[item]
    },
    // 新建
    handleCreated() {
      this.init()
      this.dialogVisible = true
      this.status = 'created'
      // this.$router.push({ path: this.$filterPath('escrowManage'), query: { type: 'c' }})
    },
    // 编辑
    handleUpdate(row) {
      for (const key in this.honorInfo) {
        this.honorInfo[key] = row[key]
      }
      console.log(this.honorInfo, 'honorInfo')
      this.init()
      this.honorInfo['id'] = row.id
      this.status = 'update'
      this.dialogVisible = true
    },
    handleRelation() {
      this.dialogVisible = true
    },
    select(props) {
      this.chooseHonor = props.chooseHonor
      this.honorInfo.honorName = props.arr.honorName
      this.honorInfo.honorId = props.arr.id
    },
    closeDialog() {
      for (const key in this.honorInfo) {
        this.honorInfo[key] = ''
      }
    },
    /**
     *多选
     */
    handleSelectionChange(arr) {
      this.multipleSelection = arr
    },
    getHonor() {
      if (this.status === 'created') {
        this.chooseHonor = true
      }
    },
    getData() {
      this.chooseResources = true
    },

    /**
     * 选择资源
     */
    selectMany(arr) {
      console.log(arr, 'arr')
      if (arr.length > 0) {
        this.honorInfo.dataIds = arr.map(item => {
          if (this.seriesType === 'RYTJ_ZDZB') {
            return {
              dataId: item.id,
              dataName: item.liveName
            }
          } else if (this.seriesType === 'RYTJ_ZDTP') {
            return {
              dataId: item.id,
              dataName: item.votingName
            }
          } else {
            return {
              dataId: item.id,
              dataName: item.name
            }
          }
        })
        console.log(this.honorInfo.dataIds, 8888)
      }
    },
    /**
     * 新建 编辑关联条件
     */
    handleSubmit() {
      const params = Object.assign({}, this.honorInfo)
      params['seriesId'] = this.seriesId
      if (this.fixed) {
        if (this.honorInfo.dataIds.length < 1) {
          this.$message({
            message: '请选择需要的数据！',
            type: 'error'
          })
          return false
        } else {
          params['dataIds'] = JSON.stringify(params['dataIds'])
          params['finishConditions'] = 2
        }
      } else {
        delete params.dataIds
      }
      delete params.honorName
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      const url = this.status === 'created' ? '/backend/honorSeriesRelevance/insert' : '/backend/honorSeriesRelevance/update'
      return new Promise((resolve, reject) => {
        RelevanceInsert(url, params)
          .then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
            this.dialogVisible = false
            this.status = ''
            this.getPage()
          })
          .catch((error) => {
            loading.close()
            reject(error)
          })
      })
    },
    /**
     * 批量删除
     */
    handleDeletes() {
      const arr = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item) => {
          arr.push(item.resourcesId)
        })
        const ids = arr.join(',')
        this.handleDelMore(ids)
      } else {
        this.$message({
          message: '请选择需要删除的数据！',
          type: 'warning'
        })
      }
    },

    handleDelMore(ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            deleteMoreResource({
              ids: ids
            })
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getPage()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch(() => {})
    },
    handleBack() {
      this.$router.back()
    },
    /**
     * 删除
     */
    handleDel(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            RelevanceDel({
              id: row.id
            })
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getPage()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch(() => {})
    },
    /**
     * 选择资源
     */
    handleSelect() {
      this.chooseResources = false
    },
    /**
     * 取消选择
     */
    handleClose() {
      this.chooseResources = false
      this.honorInfo.dataIds = []
    },
    handleDelTag(tag) {
      this.honorInfo.dataIds.splice(this.honorInfo.dataIds.indexOf(tag), 1)
    },
    /**
     * 选项卡切换
     */
    handleTabClick(tab, event) {
      setTimeout(() => {
        this.tag = this.tagList[this.activeTab]
        this.listQuery.page = 1
        this.getPage()
      }, 500)
    },

    /**
     * 获取列表
     */
    getPage(obj) {
      this.listLoading = true
      var query = {
        seriesId: this.seriesId,
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit
      }
      if (this.searchShow) {
        query['paramName'] = this.listQuery.paramName
      }
      return new Promise((resolve, reject) => {
        RelevancePage(query)
          .then((response) => {
            this.listLoading = false
            this.list = response.data.data.rows
            this.total = response.data.data.total
          })
          .catch((error) => {
            this.listLoading = false
            reject(error)
          })
      })
    },

    /**
     *搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getPage()
    },

    /**
     *重置
     */
    handleReset() {
      this.listQuery.paramName = ''
      this.listQuery.title = ''
      this.listQuery.page = 1
      this.getPage()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
$dark_gray: #bfced6;
.join-condition {
  padding: 10px;

  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor input {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .page-btn {
    display: flex;
    height: 46px;
    line-height: 46px;
    width: 100%;

    .page-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }

  .page-tabs {
    width: 100%;
    margin: 10px 0;
    padding: 0 10px;
    background-color: #fff;
    border-radius: 4px;

    .el-tabs__nav-next {
      line-height: 40px;
    }

    .el-tabs__nav-wrap::after {
      display: none;
    }

    .el-tabs__header {
      margin: 0;
    }
  }

  .page-main-list {
    .el-image {
      width: auto;
      height: 100px;
      max-height: 100px;
    }
  }

  .pagination-container {
    background: none;
  }

}
.condition{
  .esccrow-form{
    .select-data{
        display: inline-block;
        width: 120px;
        background: rgba(255, 255, 255, 1);
        @include border-color(1px, dotted, $mainColor);
        border-radius: 4px;
        font-size: 14px;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        @include font-color($mainColor);
        text-align: center;
        cursor: pointer;
    }
    .el-tag{
      margin-right: 10px;
    }
  }
}

</style>
